@model NewsContentModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.ContentManagement.News.NewsItems").Text;
    //active menu item (system name)
    NopHtml.SetActiveMenuItemSystemName("News items");
}

@{
    const string hideSearchBlockAttributeName = "NewsContentPage.HideSearchBlock";
    var hideSearchBlock = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideSearchBlockAttributeName);
}

<div class="content-header clearfix">
    <h1 class="float-left">
        @T("Admin.ContentManagement.News.NewsItems")
    </h1>
    <div class="float-right">
        <a asp-action="NewsItemCreate" class="btn btn-primary">
            <i class="fas fa-square-plus"></i>
            @T("Admin.Common.AddNew")
        </a>
        @await Component.InvokeAsync(typeof(AdminWidgetViewComponent), new { widgetZone = AdminWidgetZones.NewsListButtons, additionalData = Model })
    </div>
</div>

<section class="content">
    <div class="container-fluid">
    <div class="form-horizontal">
        <div class="cards-group">
            @*hide the entire search block if no elements are displayed*@
            <div class="card card-default card-search">
                <div class="card-body">
                    <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                        <div class="search-text">@T("Admin.Common.Search")</div>
                        <div class="icon-search"><i class="fas fa-magnifying-glass" aria-hidden="true"></i></div>
                        <div class="icon-collapse"><i class="far fa-angle-@(!hideSearchBlock ? "up" : "down")" aria-hidden="true"></i></div>
                    </div>

                    <div class="search-body @(hideSearchBlock ? "closed" : "")">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group row" @(Model.NewsItems.HideStoresList ? Html.Raw("style=\"display:none\"") : null)>                                    
                                    <div class="col-md-4">
                                        <nop-label asp-for="NewsItems.SearchStoreId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="NewsItems.SearchStoreId" asp-items="Model.NewsItems.AvailableStores" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchTitle" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchTitle" />
                                    </div>
                                </div>                                
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-8 offset-md-4">
                                <button type="button" id="search-newsitem" class="btn btn-primary btn-search">
                                    <i class="fas fa-magnifying-glass"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card card-default">
                <div class="card-body">
                    <nop-doc-reference asp-string-resource="@T("Admin.Documentation.Reference.News", Docs.News + Utm.OnAdmin)" />

                    @await Html.PartialAsync("Table", new DataTablesModel
                    {
                        Name = "newsitem-grid",
                        UrlRead = new DataUrl("List", "News", null),
                        SearchButtonId = "search-newsitem",
                        Length = Model.NewsItems.PageSize,
                        LengthMenu = Model.NewsItems.AvailablePageSizes,
                        Filters = new List<FilterParameter>
                        {
                            new FilterParameter(nameof(Model.NewsItems.SearchStoreId), nameof(Model.NewsItems)),
                            new FilterParameter(nameof(Model.SearchTitle))
                        },
                        ColumnCollection = new List<ColumnProperty>
                        {
                            new ColumnProperty(nameof(NewsItemModel.Title))
                            {
                                Title = T("Admin.ContentManagement.News.NewsItems.Fields.Title").Text
                            },
                            new ColumnProperty(nameof(NewsItemModel.LanguageName))
                            {
                                Title = T("Admin.ContentManagement.News.NewsItems.Fields.Language").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(NewsItemModel.Id))
                            {
                                Title = T("Admin.ContentManagement.News.NewsItems.Fields.Comments").Text,
                                Width = "200",
                                ClassName =  NopColumnClassDefaults.Button,
                                Render = new RenderCustom("renderColumnComments")
                            },
                            new ColumnProperty(nameof(NewsItemModel.StartDateUtc))
                            {
                                Title = T("Admin.ContentManagement.News.NewsItems.Fields.StartDate").Text,
                                Width = "200",
                                Render = new RenderDate()
                            },
                            new ColumnProperty(nameof(NewsItemModel.EndDateUtc))
                            {
                                Title = T("Admin.ContentManagement.News.NewsItems.Fields.EndDate").Text,
                                Width = "200",
                                Render = new RenderDate()
                            },
                            new ColumnProperty(nameof(NewsItemModel.Published))
                            {
                                Title = T("Admin.ContentManagement.News.NewsItems.Fields.Published").Text,
                                Width = "100",
                                ClassName =  NopColumnClassDefaults.CenterAll,
                                Render = new RenderBoolean()
                            },
                            new ColumnProperty(nameof(NewsItemModel.CreatedOn))
                            {
                                Title = T("Admin.ContentManagement.News.NewsItems.Fields.CreatedOn").Text,
                                Width = "200",
                                Render = new RenderDate()
                            },
                            new ColumnProperty(nameof(NewsItemModel.Id))
                            {
                                Title = T("Admin.Common.Edit").Text,
                                Width = "100",
                                ClassName =  NopColumnClassDefaults.Button,
                                Render = new RenderButtonEdit(new DataUrl("~/Admin/News/NewsItemEdit"))
                            }
                        }
                    })

                    <script>
                        function renderColumnComments(data, type, row, meta) {
                            var link = "NewsComments?filterByNewsItemId=" + data;
                            var title = '@T("Admin.ContentManagement.News.NewsItems.Fields.Comments").Text';
                            var countComments = row.ApprovedComments + row.NotApprovedComments;
                            return '<a class="btn btn-default" href="' + link + '">' + title + ' - ' + countComments + ' (' + row.NotApprovedComments + ')' + '</a>';
                        }
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
</section>